<template>
  <el-dialog
    title="管理标签"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-table 
      :data="labels"
      v-loading="dataListLoading"
      style="width: 100%;">
       <el-table-column
       prop="labelName"
        header-align="center"
        align="center"
        label="标签名称">
       <template scope="scope">
        <el-input size="small" v-model="scope.row.labelName" @keyup.enter.native="updateHandle(scope.row.labelId,scope.row.labelName)"></el-input>
    </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="100"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="openOrCloseInput(scope.row)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.labelId,scope.row.isDefault)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
data () {
    return {
      visible: false,
      labels:[{
        labelId:'00000000000000000000000000000000',
        labelName:'默认',
        isDefault:false
      }],
      dataListLoading: false,
    }
  },
  methods:{
    // 初始化
    init () {
      this.visible = true
      this.$http({
          url: this.$http.adornUrl('/disk/label/list'),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 200) {
            this.labels = data.data;
            console.log(this.labels);
          }
        })
    },
      // 重命名标签
    updateHandle(id,name){
    this.$http({
              url: this.$http.adornUrl('/disk/label/rename'),
              method: 'post',
              data: this.$http.adornData({
                labelId:id,
                name:name
              })
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  // duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
    },
    // 删除标签
    deleteHandle(id,isDefault){
      // console.log(id,isDefault)
      if(isDefault === false){
        this.$confirm(`确定进行删除操作吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/disk/label/delete'),
            method: 'post',
            data: this.$http.adornData({labelId:id})
          }).then(({data}) => {
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.init()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }else{
        this.$message.error('该标签不可删除')
      }
    },
    // 退出
    submit(){
      this.visible = false
      this.$emit('refreshLabelList')
    }
  }
}
</script>

<style>

</style>